<?php

namespace app\shop\controller;

use \think\Controller;
use \think\Db;

class Active extends Base
{


    /**
     * 满减活动新建之前判断：newManjianStatus
     * @return array 返回数组
     */
    public function newManjianStatus()
    {
        extract(input());
        $auth = $this->checkAll($user_id, $time, $value, $reqKey);
        if ($auth == false) {
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $today = date('Y-m-d');
        $mjyh = Db::table('up_shop_active')
            ->where('sa_shopid', $user_id)
            ->where('sa_start', '<=', $today)
            ->where('sa_end', '>=', $today)
            ->find();
        if($mjyh){
            return json_encode(['status' => 0,'msg'=>'您已经有进行中的活动']);
        }else{
            return json_encode(['status' => 200]);
        }
    }
    /**
     * 满减活动新建:newManjian
     * @return array 返回数组
     * $start，$end，$rules
     */
    public function newManjian()
    {
        extract(input());
        $auth = $this->checkAll($user_id, $time, $value, $reqKey);
        if ($auth == false) {
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = array();
        $data['sa_shopid'] = $user_id;
        $data['sa_start'] = $start;
        $data['sa_end'] = $end;
//        $rules = '300-10,60-10,50-10,20-10,100-50,150-20,30-15,25-20';
        $rules = trim($rules,',');
        $rule_array = explode(',',$rules);
        for($i=0;$i<count($rule_array)-1;$i++){
            $v = $rule_array[$i];
            $new = explode('-',$v);
            $flag = $new[0];
            for ($j=$i+1;$j<count($rule_array);$j++){
                $v2 = $rule_array[$j];
                $new2 = explode('-',$v2);
                $flag2 = $new2[0];
                if($flag2 < $flag){
                    $flag = $flag2;
                    $v = $rule_array[$i];
                    $rule_array[$i] = $v2;
                    $rule_array[$j] = $v;
                }
            }
        }
        $rulestr = '';
        foreach ($rule_array as $k=>$v){
            $new3 = explode('-',$v);
            $rulestr .= '满'.$new3[0].'减'.$new3[1].'，';
        }
        $rule = implode($rule_array,',');
        $rulestr = trim($rulestr,',');
        $data['sa_rule'] = $rule;
        $data['sa_rule_con'] = $rulestr;

        $mjyh = Db::table('up_shop_active')->insert($data);
        if($mjyh){
            return json_encode(['status' => 200,'msg'=>'新建成功']);
        }else{
            return json_encode(['status' => 0,'msg'=>'新建失败']);
        }
    }

    /**
     * 删除某个活动：delActive
     * @return string
     */
    public function delActive()
    {
        extract(input());
        $mjyh = Db::table('up_shop_active')->where('sa_id',$sa_id)->delete();
        if($mjyh){
            return json_encode(['status' => 200,'msg'=>'成功']);
        }else{
            return json_encode(['status' => 0,'msg'=>'失败']);
        }
    }
    /**
     * 删除某个秒杀活动：delKill
     * @return string
     */
    public function delKill()
    {
        extract(input());
        $goods = Db::table('up_goods')
            ->where('g_time',$g_time)
            ->field('g_kill_start,g_kill_end,g_time')
            ->column('g_id');
        $update['g_kill_start'] = '';
        $update['g_kill_end'] = '';
        $update['g_prcie_kill'] = 0;
//        $update['g_time'] = '';
        $mjyh = Db::table('up_goods')
            ->where('g_id','in',$goods)
            ->update($update);
        if($mjyh){
            return json_encode(['status' => 200,'msg'=>'成功']);
        }else{
            return json_encode(['status' => 0,'msg'=>'失败']);
        }
    }
    /**
     * 获取某个活动详情:getActive
     * @return string
     */
    public function getActive()
    {
        extract(input());
        $mjyh = Db::table('up_shop_active')->where('sa_id',$sa_id)->find();
        return json_encode(['status' => 200,'msg'=>$mjyh]);
    }
    /**
     * 修改某个活动:updateActive
     * @return string
     */
    public function updateActive()
    {
        extract(input());
        $auth = $this->checkAll($user_id, $time, $value, $reqKey);
        if ($auth == false) {
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = array();
        $data['sa_shopid'] = $user_id;
        $data['sa_start'] = $start;
        $data['sa_end'] = $end;
//        $rules = '300-10,60-10,50-10,20-10,100-50,150-20,30-15,25-20';
        $rules = trim($rules,',');
        $rule_array = explode(',',$rules);
        for($i=0;$i<count($rule_array)-1;$i++){
            $v = $rule_array[$i];
            $new = explode('-',$v);
            $flag = $new[0];
            for ($j=$i+1;$j<count($rule_array);$j++){
                $v2 = $rule_array[$j];
                $new2 = explode('-',$v2);
                $flag2 = $new2[0];
                if($flag2 < $flag){
                    $flag = $flag2;
                    $v = $rule_array[$i];
                    $rule_array[$i] = $v2;
                    $rule_array[$j] = $v;
                }
            }
        }
        $rulestr = '';
        foreach ($rule_array as $k=>$v){
            $new3 = explode('-',$v);
            $rulestr .= '满'.$new3[0].'减'.$new3[1].'，';
        }
        $rule = implode($rule_array,',');
        $rulestr = trim($rulestr,',');
        $data['sa_rule'] = $rule;
        $data['sa_rule_con'] = $rulestr;

        $mjyh = Db::table('up_shop_active')->where('sa_id',$sa_id)->update($data);
        if($mjyh){
            return json_encode(['status' => 200,'msg'=>'成功']);
        }else{
            return json_encode(['status' => 0,'msg'=>'失败']);
        }
    }
    /**
     * 秒杀活动新建前获取商品或者判断可以不可以新建：getKillGoods
     * @return array 返回数组
     */
    public function getKillGoods()
    {
        extract(input());
        $auth = $this->checkAll($user_id, $time, $value, $reqKey);
        if ($auth == false) {
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $goods = Db::table('up_goods')
            ->where('g_shop',$user_id)
            ->where('g_prcie_kill',0)
            ->where('g_type', $typeid)
            ->where('g_status',1)
            ->select();
        return json_encode(['status' => 200,'msg'=>$goods]);
    }
    /**
     * 秒杀活动新建
     * start，end，kill，goodsid (id集合)
     * @return array 返回数组
     */
    public function newKillGoods()
    {
        extract(input());
        $auth = $this->checkAll($user_id, $time, $value, $reqKey);
        if ($auth == false) {
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = array();
        $data['g_kill_start'] = $start;
        $data['g_kill_end'] = $end;
        $data['g_prcie_kill'] = $kill;
        $data['g_time'] = date('Y-m-d H:i:s');
        $goodsid = trim($goodsid,',');
        $res = Db::table('up_goods')
            ->where('g_id','in',$goodsid)
            ->update($data);
        if($res){
            return json_encode(['status' => 200,'msg'=>'操作成功']);
        }else{
            return json_encode(['status' => 0,'msg'=>'操作失败']);
        }
    }


    /**
     * 获取我创建的满减和秒杀:myActive
     * type:1未开始  2进行中  3已结束
     * @return array 返回数组
     */
    public function myActive()
    {
        extract(input());
        $auth = $this->checkAll($user_id, $time, $value, $reqKey);
        if ($auth == false) {
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $today = date('Y-m-d');
        //------kill的条件--------
        $data = array();
        $data['g_shop'] = $user_id;
        $data['g_status'] = 1;
        $data['g_prcie_kill'] = array('>',0);
        //------kill的条件end-----
        if($type == 1){
            $active = Db::table('up_shop_active')
                ->where('sa_shopid',$user_id)
                ->where('sa_start','>',$today)
                ->select();
            $kill = Db::table('up_goods')
                ->where($data)
                ->where('g_kill_start','>',$today)
                ->group('g_time')
                ->field('g_kill_start,g_kill_end,g_time')
                ->select();
            if($kill){
                foreach ($kill as $k=>$v){
                    $kill[$k]['goods'] = Db::table('up_goods')
                        ->where($data)
                        ->where('g_kill_start','>',$today)
                        ->where('g_time',$v['g_time'])
                        ->field('g_id,g_name,g_price_before,g_price_now,g_prcie_kill')
                        ->select();
                }
            }
        }elseif ($type == 2){
            $active = Db::table('up_shop_active')
                ->where('sa_shopid',$user_id)
                ->where('sa_start','<=',$today)
                ->where('sa_end','>=',$today)
                ->select();
            $kill = Db::table('up_goods')
                ->where($data)
                ->where('g_kill_start','<=',$today)
                ->where('g_kill_end','>=',$today)
                ->group('g_time')
                ->field('g_kill_start,g_kill_end,g_time')
                ->select();
            if($kill){
                foreach ($kill as $k=>$v){
                    $kill[$k]['goods'] = Db::table('up_goods')
                        ->where($data)
                        ->where('g_kill_start','<=',$today)
                        ->where('g_kill_end','>=',$today)
                        ->where('g_time',$v['g_time'])
                        ->field('g_id,g_name,g_price_before,g_price_now,g_prcie_kill')
                        ->select();
                }
            }
        }else{
            $active = Db::table('up_shop_active')
                ->where('sa_shopid',$user_id)
                ->where('sa_end','<',$today)
                ->select();
            $kill = Db::table('up_goods_kill')
                ->group('g_time')
                ->field('g_kill_start,g_kill_end,g_time')
                ->select();
            if($kill){
                foreach ($kill as $k=>$v){
                    $kill[$k]['goods'] = Db::table('up_goods_kill')
                        ->where('g_time',$v['g_time'])
                        ->field('g_id,g_name,g_price_before,g_price_now,g_prcie_kill')
                        ->select();
                }
            }
        }

        return json_encode(['status' => 200,'active'=>$active,'kill'=>$kill]);
    }

    /**
     * 广告：myAdvert
     */
    public function myAdvert()
    {
        extract(input());
        $auth = $this->checkAll($user_id, $time, $value, $reqKey);
        if ($auth == false) {
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = Db::table('up_shop_advert')
            ->where('adv_shopid',$user_id)
            ->select();
        return json_encode(['status' => 200,'msg'=>$data]);
    }
    public function addAdvert()
    {
        extract(input());
        $auth = $this->checkAll($user_id, $time, $value, $reqKey);
        if ($auth == false) {
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = array();
        $data['adv_shopid'] = $user_id;
        $data['adv_image'] = $image;
        $data['adv_title'] = $title;
        $data['adv_desc'] = $desc;
        $data['adv_g_id'] = $gid;
        $res = Db::table('up_shop_advert')
            ->insert($data);
        if($res){
            return json_encode(['status' => 200,'msg'=>'操作成功']);
        }else{
            return json_encode(['status' => 0,'msg'=>'操作失败']);
        }
    }
    public function editAdvert()
    {
        extract(input());
        $auth = $this->checkAll($user_id, $time, $value, $reqKey);
        if ($auth == false) {
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = array();
        $data['adv_image'] = $image;
        $data['adv_title'] = $title;
        $data['adv_desc'] = $desc;
        $data['adv_g_id'] = $gid;
        $res = Db::table('up_shop_advert')
            ->where('adv_id',$adv_id)->update($data);
        if($res){
            return json_encode(['status' => 200,'msg'=>'操作成功']);
        }else{
            return json_encode(['status' => 0,'msg'=>'操作失败']);
        }
    }
    public function delAdvert()
    {
        extract(input());
        $auth = $this->checkAll($user_id, $time, $value, $reqKey);
        if ($auth == false) {
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $res = Db::table('up_shop_advert')
            ->where('adv_id',$adv_id)->delete();
        if($res){
            return json_encode(['status' => 200,'msg'=>'操作成功']);
        }else{
            return json_encode(['status' => 0,'msg'=>'操作失败']);
        }
    }

}